<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['ischecklogin']=$this->model('member')->checklogin('','','','',true);
$_SESSION['uniacid']=$_SESSION['uniacid'];
if($_RQ['op']=='main'){
	$getlogid=intval($_RQ['getlogid']);
	$log=pdo_get('jy_weishop_coupon_getlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$getlogid));
	if (empty($log)){
		exi('优惠券错误,无权核销','error');   
	}
	$coupon=$this->model('coupon')->intcoupon($log['couponid'],$log['createtime']);
	$url=$coupon['merchid']?'jy_merch/verify.index/':'jy_weishop/verify.index/';
	if($coupon['coupontype']!=1){
		exi('优惠券类型错误,无权核销','error',$url);
	}
	if($coupon['isuse']==1){
		exi('优惠券已使用,无权核销','error',$url);
	}
	if($coupon['past']==1){
		exi('优惠券已过期,无权核销','error',$url);
	}
	if(!$coupon['merchid']){
		$store=pdo_get('jy_weishop_store',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if (empty($saler) && !$store){
			$prams=array('status'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
			$saler=pdo_get('jy_weishop_store_worker',$prams);
			if(!$saler['id']){
				exi('您不是门店员工,无权核销','error',$url);
			} 
		}
	}else{
		$merch=model('jy_merch/verify')->checkuser($_SESSION['uid'],$coupon['merchid'],'',false);
		if(!$merch['id']){
			exi('该优惠券属于商户，您不是该商户的店员,无权核销','error',$url);   
		}
	}
	$coupon['limitgoodids']= explode(';', $coupon['limitgoodids']);
	$coupon['limitgoodids']= substr(implode(',', $coupon['limitgoodids']),1,strlen(implode(',', $coupon['limitgoodids']))-2);
	$coupon['limitgoodcateids']= explode(';', $coupon['limitgoodcateids']);
	$coupon['limitgoodcateids']= substr(implode(',', $coupon['limitgoodcateids']),1,strlen(implode(',', $coupon['limitgoodcateids']))-2);
	if ($coupon['limitgoodtype'] != 0) {
		if (!empty($coupon['limitgoodids'])) {
			$where = 'and id in(' . $coupon['limitgoodids'] . ')';
		}
		$goods = pdo_fetchall('select `title` from ' . tablename('jy_weishop_goods') . ' where uniacid=:uniacid ' . $where, array(':uniacid' => $_SESSION['uniacid']), 'id');
	}
	if ($coupon['limitgoodcatetype'] != 0) {
		if (!empty($coupon['limitgoodcateids'])) {
			$where = 'and id in(' . $coupon['limitgoodcateids'] . ')';
		}
		$group = pdo_fetchall('select `title`  from ' . tablename('jy_weishop_goods_group') . ' where uniacid=:uniacid   ' . $where, array(':uniacid' => $_SESSION['uniacid']), 'id');
	}
	$return['setting']=$this->plugin_setting('coupon');
	$return['goods']=$goods;
	$return['group']=$group;
	$return['post']=array(
		'getlogid'=>$log['id'],
		'merchid'=>$coupon['merchid'],
	);
	$return['log']=$log;
	$return['coupon']=$coupon;
	set_html('title','核销优惠券');
    exi($return);
}

if($_RQ['op']=='post'){
	$post=get_postdata();
	$getlogid=intval($post['getlogid']);
	$merchid=intval($post['merchid']);
	$this->model('coupon')->usecoupon($getlogid,'hexiao');
	if($merchid){
		exi('核销成功！','','jy_merch/verify.index/');
	}else{
		exi('核销成功！','','jy_weishop/verify.index/');
	}
}